package org.luxor.commons.selenium;

import org.luxor.commons.core.utils.UuidUtils;
import org.luxor.commons.selenium.component.Context;
import org.luxor.commons.selenium.component.SeleniumDriver;
import org.luxor.commons.selenium.component.command.SeleniumCommand;
import org.luxor.commons.selenium.listener.AbstractSeleniumEventListener;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Selenium 事件监听器抽象类
 *
 * @author Mr.yan  @date 2022/4/10
 */
@Component
public class MeSeleniumEventListener extends AbstractSeleniumEventListener {
    private static final Logger log = LoggerFactory.getLogger(MeSeleniumEventListener.class);

    /**
     * 初始化指令之前
     *
     * @param command 业务指令
     * @param context 业务上下文
     * @return void
     */
    @Override
    public void init(SeleniumCommand command, Context context) {
        context.setTrackId(UuidUtils.uuid32());
        log.info("Enter init Event. ");
    }

    /**
     * 指令执行之前
     *
     * @param webDriver 驱动器
     * @param command   业务指令
     * @param context   业务上下文
     * @return void
     */
    @Override
    public void beforeExecute(SeleniumDriver webDriver, SeleniumCommand command, Context context) {
        log.info("Enter beforeExecute Event. ");
    }

    /**
     * 指令执行之后
     *
     * @param webDriver 驱动器
     * @param command   业务指令
     * @param context   业务上下文
     * @return void
     */
    @Override
    public void afterExecute(SeleniumDriver webDriver, SeleniumCommand command, Context context) {
        log.info("Enter afterExecute Event.");
    }

    /**
     * 指令执行异常
     *
     * @param throwable 驱动器
     * @param driver    驱动器
     * @return void
     */
    @Override
    public void onException(Throwable throwable, WebDriver driver) {
        log.error("Enter onException Event.");
    }

}
